package com.edeweiss.community.dao.daoImp;

import com.edeweiss.community.dao.AdminDao;
import com.edeweiss.community.domain.Admin;
import com.edeweiss.community.domain.Hof;
import com.edeweiss.community.domain.HonorWall;
import com.edeweiss.community.web.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.List;

public class AdminDaoImp implements AdminDao {
    QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

    @Override
    public Admin login(Admin admin) {
        String sql="select * from manager where mid=? and password=?";
        Object[] param = {admin.getMid(),admin.getPassword()};
        try {
            Admin query = qr.query(sql, new BeanHandler<Admin>(Admin.class),param);
            return query;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //查询处理未处理的申请
    @Override
    public List<HonorWall> findHonorApprove() {
        String sql="select * from honorwall where status = 0";
        List<HonorWall> honorWalls = null;
        try {
             honorWalls = qr.query(sql, new BeanListHandler<HonorWall>(HonorWall.class));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return honorWalls;
    }

    @Override
    public List<HonorWall> findDissolveCommunity() {
        //查询处理未处理的申请
        String sql="select * from dissolve_community where status = 0";
        List<HonorWall> honorWalls = null;
        try {
            honorWalls = qr.query(sql, new BeanListHandler<HonorWall>(HonorWall.class));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return honorWalls;
    }

    @Override
    public int honorApprove(int cid, int result) {
        int i = 0;
        String msg = result==1?"恭喜，通过了荣誉墙申请！":"申请失败，请下次再试！";
        Connection conn = JDBCUtils.getConnection();
        String sql="update honorwall set result = ?,status = 1 where community_id = ?";
        String sql1="insert into community_message(cid,message,status,time) value (?,?,?,?)";
        try {
            JDBCUtils.startTransaction(conn);
            qr.update(conn,sql, result, cid);
            qr.update(conn,sql1,cid,msg,0,new Date());
            JDBCUtils.commitTransaction(conn);
            i = 1;
        }catch (Exception e) {
            JDBCUtils.rollbackTransaction(conn);
            e.printStackTrace();
        }
        return i;
    }
    @Override
    public int dissolveCommunity(int cid, int result) {
        Connection conn = JDBCUtils.getConnection();
        String sql = "delete from community where cid = ?";
        String sql1 = "update dissolve_community set status = ?,result = ? where community_id = ?";
        int i = 0;
        try {
            if(result == 0){
                //不同意解散
                qr.update(sql1,1,result,cid);
                i = 1;
            }else{
                //开启事务
                JDBCUtils.startTransaction(conn);
                PreparedStatement st = conn.prepareStatement(sql);
                st.setInt(1,cid);
                st.execute();
                st = conn.prepareStatement(sql1);
                st.setInt(1,1);
                st.setInt(2,result);
                st.setInt(3,cid);
                st.execute();
                //提交事务
                JDBCUtils.commitTransaction(conn);
                i = 1;
            }
        }catch (Exception e) {
            //回滚事务
            JDBCUtils.rollbackTransaction(conn);
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public List<Hof> findHof() {
        //查询处理未处理的申请
        String sql="select * from hof where status = 0";
        List<Hof> hofs = null;
        try {
            hofs = qr.query(sql, new BeanListHandler<Hof>(Hof.class));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return hofs;
    }

    @Override
    //处理申请名人堂的请求
    public int handlerHof(int hid, int result,String sid) {
        int i = 0;
        String message = result==1?"通过名人堂申请":"管理员拒绝的您的名人堂申请";

        String sql1="update hof set result = ?,status = 1 where hid = ?";
        String sql2="insert into message (sid,message,status,time) values (?,?,?,?)";
        Connection conn = JDBCUtils.getConnection();

        try {
            JDBCUtils.startTransaction(conn);
            qr.update(conn,sql1, result, hid);
            qr.update(conn,sql2,sid,message,0,new Date());
            JDBCUtils.commitTransaction(conn);
            i = 1;
        }catch (Exception e) {
            JDBCUtils.rollbackTransaction(conn);
            e.printStackTrace();
        }
        return i;
    }

}
